/**
 * @author gaojiao
 * @date 2018/1/30
 */
'use strict';
angular.module('app')
    .controller('readCountConfigController', [ '$rootScope', '$scope', '$http', '$state',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '阅读量参数配置';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};


            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/postReadCountConfig/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {

                        $scope.pageInfo = result.data;

                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }



          /*  $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/postReadCountConfig/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                        $(".editSortBtn").show();
                        $(".saveSortBtn").hide();
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
*/
            $scope.search();


            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };


            $scope.oper = function (id,$event) {

                $.ajax({
                    url : $scope.app.project_name+'/postReadCountConfig/updateEnable',
                    data: {id:id,isEnable:$event.target.checked?'Y':'N',state:$event.target.checked?'N':'Y'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }

            // 编辑排序
            $scope.editSort=function (configNo) {
                document.getElementById("clickMinNum"+configNo).style.display="none";
                document.getElementById("btnMinNum"+configNo).style.display="inline";

                document.getElementById("clickMaxNum"+configNo).style.display="none";
                document.getElementById("btnMaxNum"+configNo).style.display="inline";

                document.getElementById("clickRandomMinNum"+configNo).style.display="none";
                document.getElementById("btnRandomMinNum"+configNo).style.display="inline";

                document.getElementById("clickRandomMaxNum"+configNo).style.display="none";
                document.getElementById("btnRandomMaxNum"+configNo).style.display="inline";

                document.getElementById("editStart"+configNo).style.display="none";
                document.getElementById("editEnd"+configNo).style.display="inline";
            }


            $scope.edit=function (id,minNum,maxNum,randomMinNum,randomMaxNum,postType) {
                 var minNum=$("#minNum"+id).val();
                 var maxNum=$("#maxNum"+id).val();
                 var randomMinNum=$("#randomMinNum"+id).val();
                 var randomMaxNum=$("#randomMaxNum"+id).val();

                 /*var patrn =/^[0-9]*$/;
                 if(isNaN(minNum)||minNum<=0||!(patrn.test(minNum))){
                     $scope.alertModal({'content':'最小值只能输入整数'});
                     return ;
                 }
                 if(isNaN(maxNum)||maxNum<=0||!(patrn.test(maxNum))){
                     $scope.alertModal({'content':'最大值只能输入整数'});
                     return ;
                 }*/

                 if(parseInt(maxNum) < parseInt(minNum)|| parseInt(maxNum)== parseInt(minNum)){
                     $scope.alertModal({'content':'数据不正确，阅读量最小值大于了最大值'});
                     return;
                 }

               /* if(isNaN(randomMinNum)||randomMinNum<=0||!(/^\d+$/.test(randomMinNum))){
                    $scope.alertModal({'content':'随机加数最小值只能输入整数'});
                    return ;
                }
                if(isNaN(randomMaxNum)||randomMaxNum<=0||!(/^\d+$/.test(randomMaxNum))){
                    $scope.alertModal({'content':'随机加数最大值只能输入整数'});
                    return ;
                }*/

                if(parseInt(randomMaxNum) < parseInt(randomMinNum)|| parseInt(randomMaxNum)== parseInt(randomMinNum)){
                    $scope.alertModal({'content':'数据不正确，随机加数最小值大于了随机加数最大值'});
                    return;
                }
                 saveData(id,minNum,maxNum,randomMinNum,randomMaxNum,postType);
             }

            function saveData(id,minNum,maxNum,randomMinNum,randomMaxNum,postType){
                $.ajax({
                    url : $scope.app.project_name+'/postReadCountConfig/update' ,
                    data: {"configNo":id,"minNum":minNum,"maxNum":maxNum,"randomMinNum":randomMinNum,"randomMaxNum":randomMaxNum,"postType":postType}
                }).then(callback);
                function callback(result){
                    if(result.httpCode==200 ) {
                        $scope.alertModal({"content":"操作成功！"});
                        $scope.search();
                    } else{
                        $scope.alertModal({"content":"操作失败！"});
                    }
                    $scope.loading = false;
                }
            }

        } ]);